#include "Coin.h"
#include "ResourceManager.h"
#include <d3dx9.h>

Coin::Coin(int id,float x, float y, int type, int state)
{
	if(type == 14)
	{
		_sprite = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Item),0, 0);
		_sprite ->SetFrame(6,6);
		_numberCoin = 1;
		isHidden = false;
	}
	if(type ==39)
	{
		_sprite = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Item),0, 0);
		_sprite ->SetFrame(7,7);
		_numberCoin = 1;
		isHidden = true;
	}
			
	_id = id;
	_x = x;
	_y = y;
	_type = type;
	_state = state;
	_width = _sprite->getTexture()->getWidth();
	_height = _sprite->getTexture()->getHeight();
	_yPre = y;
	_vy = 0;
	
	_accelY = 0;
}

Coin::~Coin(void)
{
	if (_sprite)
	{
		delete _sprite;
	}
	
}

void Coin::Update(list<Object *> *_list, float time)
{
	switch (_state)
	{
	case IS_IDLE:
		{
		if (_numberCoin == 0)
		{
			changeState(IS_REMOVE);
		}
		break;
		}
	case IS_ACTIVING:
		{
		if (_numberCoin != 0)
		{
			_vy -= _accelY * time;
			_y += _vy * time + 1.0f/2.0f * _accelY * time * time;
			if ((_y > _yPre + 128) && (_vy > 0))
			{
				_numberCoin--;
				if (_numberCoin == 0)
				{
					changeState(IS_REMOVE);	
				}
					
			}
		}
		break;
		}
	}
	_sprite->Update(time);
}


void Coin:: Render(int vpX, int vpY)
{
	if (_state != IS_REMOVE)
	{
		_sprite->Render(_x, _y, vpX, vpY);
	}
	
}

void Coin::changeState(int state)
{
	_state = state;
	switch (_state)
	{
	case IS_IDLE:
		_y = _yPre;
		_vy = 0;
		_accelY = 0;
		_sprite->SetFrame(7,7);
		break;
	case IS_ACTIVING:
		_vy = 1.5;	

		_accelY = COIN_GRAVITY;
		_sprite->SetFrame(6,6);
		break;
	case  IS_REMOVE:
		break;
	}
}

OBJECT_TYPE Coin::getObjectType()
{
	return OBJECT_ITEM;
}